package com.slf.engine.threads;

import java.io.IOException;

import org.apache.log4j.Logger;
import org.apache.log4j.Priority;

import com.slf.engine.common.SysContants;
import com.slf.engine.common.SysContext;
import com.slf.engine.exception.EngineException;
import com.slf.engine.services.ITjfxConnService;
import com.slf.engine.services.impl.TjfxConnServiceImpl;
import com.slf.engine.utils.SocketUtil;

public class HeartThread extends Thread {

	private static final Logger log = Logger.getLogger(HeartThread.class);
	
	public HeartThread()
	{
		super("HEART_THREAD");
		log.log(Priority.INFO, "HEART_THREAD RUN SUCCESS");
	}
	
	public void run()
	{
		if(SysContext.FREE_TIME == 0)
		{
			SysContext.FREE_TIME = System.currentTimeMillis();
		}
		ITjfxConnService connService = new TjfxConnServiceImpl();
		while(true)
		{
			if(System.currentTimeMillis() - SysContext.FREE_TIME > 1000*20)
			{
				if(SysContext.HEART_TIME == 0)
				{
					SysContext.FREE_TIME = System.currentTimeMillis();
					log.log(Priority.INFO, "开始执行心跳访问");
					SysContext.HEART_TIME = System.currentTimeMillis();
					if(!connService.loginServer())
					{
						SysContext.IS_START = false;
						SocketUtil.closeSocket();
						connService.loginServer();
					}
				}else if(System.currentTimeMillis() - SysContext.HEART_TIME > 10*1000){
					SysContext.IS_START = false;
					SocketUtil.closeSocket();
					connService.loginServer();
					try {
						Thread.sleep(5*1000);
					} catch (InterruptedException e) {
						// TODO Auto-generated catch block
					}
				}
			}else{
				try {
					Thread.sleep(30*1000);
				} catch (InterruptedException e) {
					// TODO Auto-generated catch block
				}
			}
		}
	}
}
